home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTAAUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  15.0 KB  |  575 lines

  1.  
  2. /* MODULE                            HTAAUtil.c
  3. **        COMMON PARTS OF ACCESS AUTHORIZATION MODULE
  4. **            FOR BOTH SERVER AND BROWSER
  5. **
  6. ** IMPORTANT:
  7. **    Routines in this module use dynamic allocation, but free
  8. **    automatically all the memory reserved by them.
  9. **
  10. **    Therefore the caller never has to (and never should)
  11. **    free() any object returned by these functions.
  12. **
  13. **    Therefore also all the strings returned by this package
  14. **    are only valid until the next call to the same function
  15. **    is made. This approach is selected, because of the nature
  16. **    of access authorization: no string returned by the package
  17. **    needs to be valid longer than until the next call.
  18. **
  19. **    This also makes it easy to plug the AA package in:
  20. **    you don't have to ponder whether to free() something
  21. **    here or is it done somewhere else (because it is always
  22. **    done somewhere else).
  23. **
  24. **    The strings that the package needs to store are copied
  25. **    so the original strings given as parameters to AA
  26. **    functions may be freed or modified with no side effects.
  27. **
  28. **    The AA package does not free() anything else than what
  29. **    it has itself allocated.
  30. **
  31. **    AA (Access Authorization) package means modules which
  32. **    names start with HTAA.
  33. **
  34. ** AUTHORS:
  35. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  36. **    MD     Mark Donszelmann    duns@vxdeop.cern.ch
  37. **
  38. ** HISTORY:
  39. **     8 Nov 93  MD    (VMS only) Added case insensitive comparison in HTAA_templateCaseMatch
  40. **
  41. **
  42. ** BUGS:
  43. **
  44. **
  45. */
  46.  
  47. #include "HTUtils.h"
  48. #include "tcp.h"    /* NETREAD() etc.    */
  49. #include <string.h>
  50. #include "HTAAUtil.h"    /* Implemented here    */
  51. #include "HTAssoc.h"    /* Assoc list        */
  52.  
  53. #include "LYLeaks.h"
  54.  
  55. /* PUBLIC                        HTAAScheme_enum()
  56. **        TRANSLATE SCHEME NAME INTO
  57. **        A SCHEME ENUMERATION
  58. **
  59. ** ON ENTRY:
  60. **    name        is a string representing the scheme name.
  61. **
  62. ** ON EXIT:
  63. **    returns        the enumerated constant for that scheme.
  64. */
  65. PUBLIC HTAAScheme HTAAScheme_enum ARGS1(CONST char*, name)
  66. {
  67.     static char *upcased = NULL;
  68.     char *cur;
  69.  
  70.     if (!name) return HTAA_UNKNOWN;
  71.  
  72.     StrAllocCopy(upcased, name);
  73.     cur = upcased;
  74.     while (*cur) {
  75.     *cur = TOUPPER(*cur);
  76.     cur++;
  77.     }
  78.     
  79.     if (!strncmp(upcased, "NONE", 4))
  80.     return HTAA_NONE;
  81.     else if (!strncmp(upcased, "BASIC", 5))
  82.     return HTAA_BASIC;
  83.     else if (!strncmp(upcased, "PUBKEY", 6))
  84.     return HTAA_PUBKEY;
  85.     else if (!strncmp(upcased, "KERBEROSV4", 10))
  86.     return HTAA_KERBEROS_V4;
  87.     else if (!strncmp(upcased, "KERBEROSV5", 10))
  88.     return HTAA_KERBEROS_V5;
  89.     else
  90.     return HTAA_UNKNOWN;
  91. }
  92.  
  93.  
  94. /* PUBLIC                        HTAAScheme_name()
  95. **            GET THE NAME OF A GIVEN SCHEME
  96. ** ON ENTRY:
  97. **    scheme        is one of the scheme enum values:
  98. **            HTAA_NONE, HTAA_BASIC, HTAA_PUBKEY, ...
  99. **
  100. ** ON EXIT:
  101. **    returns        the name of the scheme, i.e.
  102. **            "None", "Basic", "Pubkey", ...
  103. */
  104. PUBLIC char *HTAAScheme_name ARGS1(HTAAScheme, scheme)
  105. {
  106.     switch (scheme) {
  107.       case HTAA_NONE:        return "None";          break;
  108.       case HTAA_BASIC:        return "Basic";         break;
  109.       case HTAA_PUBKEY:        return "Pubkey";        break;
  110.       case HTAA_KERBEROS_V4:    return "KerberosV4";    break;
  111.       case HTAA_KERBEROS_V5:    return "KerberosV5";    break;
  112.       case HTAA_UNKNOWN:    return "UNKNOWN";       break;
  113.       default:            return "THIS-IS-A-BUG";
  114.     }
  115. }
  116.  
  117.  
  118. /* PUBLIC                            HTAAMethod_enum()
  119. **        TRANSLATE METHOD NAME INTO AN ENUMERATED VALUE
  120. ** ON ENTRY:
  121. **    name        is the method name to translate.
  122. **
  123. ** ON EXIT:
  124. **    returns        HTAAMethod enumerated value corresponding
  125. **            to the given name.
  126. */
  127. PUBLIC HTAAMethod HTAAMethod_enum ARGS1(CONST char *, name)
  128. {
  129.     char tmp[MAX_METHODNAME_LEN+1];
  130.     CONST char *src = name;
  131.     char *dest = tmp;
  132.  
  133.     if (!name) return METHOD_UNKNOWN;
  134.  
  135.     while (*src) {
  136.     *dest = TOUPPER(*src);
  137.     dest++;
  138.     src++;
  139.     }
  140.     *dest = 0;
  141.  
  142.     if (0==strcmp(tmp, "GET"))
  143.     return METHOD_GET;
  144.     else if (0==strcmp(tmp, "PUT"))
  145.     return METHOD_PUT;
  146.     else
  147.     return METHOD_UNKNOWN;
  148. }
  149.  
  150.  
  151. /* PUBLIC                        HTAAMethod_name()
  152. **            GET THE NAME OF A GIVEN METHOD
  153. ** ON ENTRY:
  154. **    method        is one of the method enum values:
  155. **            METHOD_GET, METHOD_PUT, ...
  156. **
  157. ** ON EXIT:
  158. **    returns        the name of the scheme, i.e.
  159. **            "GET", "PUT", ...
  160. */
  161. PUBLIC char *HTAAMethod_name ARGS1(HTAAMethod, method)
  162. {
  163.     switch (method) {
  164.       case METHOD_GET:        return "GET";           break;
  165.       case METHOD_PUT:        return "PUT";           break;
  166.       case METHOD_UNKNOWN:    return "UNKNOWN";       break;
  167.       default:            return "THIS-IS-A-BUG";
  168.     }
  169. }
  170.  
  171.  
  172. /* PUBLIC                        HTAAMethod_inList()
  173. **        IS A METHOD IN A LIST OF METHOD NAMES
  174. ** ON ENTRY:
  175. **    method        is the method to look for.
  176. **    list        is a list of method names.
  177. **
  178. ** ON EXIT:
  179. **    returns        YES, if method was found.
  180. **            NO, if not found.
  181. */
  182. PUBLIC BOOL HTAAMethod_inList ARGS2(HTAAMethod,    method,
  183.                     HTList *,    list)
  184. {
  185.     HTList *cur = list;
  186.     char *item;
  187.  
  188.     while (NULL != (item = (char*)HTList_nextObject(cur))) {
  189.     if (TRACE) fprintf(stderr, " %s", item);
  190.     if (method == HTAAMethod_enum(item))
  191.         return YES;
  192.     }
  193.  
  194.     return NO;    /* Not found */
  195. }
  196.  
  197.  
  198.  
  199. /* PUBLIC                        HTAA_templateMatch()
  200. **        STRING COMPARISON FUNCTION FOR FILE NAMES
  201. **           WITH ONE WILDCARD * IN THE TEMPLATE
  202. ** NOTE:
  203. **    This is essentially the same code as in HTRules.c, but it
  204. **    cannot be used because it is embedded in between other code.
  205. **    (In fact, HTRules.c should use this routine, but then this
  206. **     routine would have to be more sophisticated... why is life
  207. **     sometimes so hard...)
  208. **
  209. ** ON ENTRY:
  210. **    template    is a template string to match the file name
  211. **            agaist, may contain a single wildcard
  212. **            character * which matches zero or more
  213. **            arbitrary characters.
  214. **    filename    is the filename (or pathname) to be matched
  215. **            agaist the template.
  216. **
  217. ** ON EXIT:
  218. **    returns        YES, if filename matches the template.
  219. **            NO, otherwise.
  220. */
  221. PUBLIC BOOL HTAA_templateMatch ARGS2(CONST char *, template, 
  222.                      CONST char *, filename)
  223. {
  224.     CONST char *p = template;
  225.     CONST char *q = filename;
  226.     int m;
  227.  
  228.     for( ; *p  &&  *q  &&  *p == *q; p++, q++)    /* Find first mismatch */
  229.     ; /* do nothing else */
  230.  
  231.     if (!*p && !*q)    return YES;    /* Equally long equal strings */
  232.     else if ('*' == *p) {        /* Wildcard */
  233.     p++;                /* Skip wildcard character */
  234.     m = strlen(q) - strlen(p);    /* Amount to match to wildcard */
  235.     if (m < 0) return NO;        /* No match, filename too short */
  236.     else {            /* Skip the matched characters and compare */
  237.         if (strcmp(p, q+m))    return NO;    /* Tail mismatch */
  238.         else                return YES;    /* Tail match */
  239.     }
  240.     }    /* if wildcard */
  241.     else        return NO;    /* Length or character mismatch */
  242. }    
  243.  
  244.  
  245. /* PUBLIC                        HTAA_templateCaseMatch()
  246. **        STRING COMPARISON FUNCTION FOR FILE NAMES
  247. **           WITH ONE WILDCARD * IN THE TEMPLATE (Case Insensitive)
  248. ** NOTE:
  249. **    This is essentially the same code as in HTAA_templateMatch, but
  250. **    it compares case insensitive (for VMS). Reason for this routine
  251. **    is that HTAA_templateMatch gets called from several places, also 
  252. **    there where a case sensitive match is needed, so one cannot just
  253. **    change the HTAA_templateMatch routine for VMS.
  254. **
  255. ** ON ENTRY:
  256. **    template    is a template string to match the file name
  257. **            agaist, may contain a single wildcard
  258. **            character * which matches zero or more
  259. **            arbitrary characters.
  260. **    filename    is the filename (or pathname) to be matched
  261. **            agaist the template.
  262. **
  263. ** ON EXIT:
  264. **    returns        YES, if filename matches the template.
  265. **            NO, otherwise.
  266. */
  267. PUBLIC BOOL HTAA_templateCaseMatch ARGS2(CONST char *, template, 
  268.                          CONST char *, filename)
  269. {
  270.     CONST char *p = template;
  271.     CONST char *q = filename;
  272.     int m;
  273.  
  274.     for( ; *p  &&  *q  &&  TOUPPER(*p) == TOUPPER(*q); p++, q++) /* Find first mismatch */
  275.     ; /* do nothing else */
  276.  
  277.     if (!*p && !*q)    return YES;    /* Equally long equal strings */
  278.     else if ('*' == *p) {        /* Wildcard */
  279.     p++;                /* Skip wildcard character */
  280.     m = strlen(q) - strlen(p);    /* Amount to match to wildcard */
  281.     if (m < 0) return NO;        /* No match, filename too short */
  282.     else {            /* Skip the matched characters and compare */
  283.         if (strcasecomp(p, q+m))    return NO;    /* Tail mismatch */
  284.         else                return YES;    /* Tail match */
  285.     }
  286.     }    /* if wildcard */
  287.     else        return NO;    /* Length or character mismatch */
  288. }    
  289.  
  290.  
  291. /* PUBLIC                    HTAA_makeProtectionTemplate()
  292. **        CREATE A PROTECTION TEMPLATE FOR THE FILES
  293. **        IN THE SAME DIRECTORY AS THE GIVEN FILE
  294. **        (Used by server if there is no fancier way for
  295. **        it to tell the client, and by browser if server
  296. **        didn't send WWW-ProtectionTemplate: field)
  297. ** ON ENTRY:
  298. **    docname    is the document pathname (from URL).
  299. **
  300. ** ON EXIT:
  301. **    returns    a template matching docname, and other files
  302. **        files in that directory.
  303. **
  304. **        E.g.  /foo/bar/x.html  =>  /foo/bar/ *
  305. **                            ^
  306. **                Space only to prevent it from
  307. **                being a comment marker here,
  308. **                there really isn't any space.
  309. */
  310. PUBLIC char *HTAA_makeProtectionTemplate ARGS1(CONST char *, docname)
  311. {
  312.     char *template = NULL;
  313.     char *slash = NULL;
  314.  
  315.     if (docname) {
  316.     StrAllocCopy(template, docname);
  317.     slash = strrchr(template, '/');
  318.     if (slash) slash++;
  319.     else slash = template;
  320.     *slash = (char)0;
  321.     StrAllocCat(template, "*");
  322.     }
  323.     else StrAllocCopy(template, "*");
  324.  
  325.     if (TRACE) fprintf(stderr,
  326.                "make_template: made template `%s' for file `%s'\n",
  327.                template, docname);
  328.  
  329.     return template;
  330. }
  331.  
  332.  
  333.  
  334.  
  335. /*
  336. ** Skip leading whitespace from *s forward
  337. */
  338. #define SKIPWS(s) while (*s==' ' || *s=='\t') s++;
  339.  
  340. /*
  341. ** Kill trailing whitespace starting from *(s-1) backwords
  342. */
  343. #define KILLWS(s) {char *c=s-1; while (*c==' ' || *c=='\t') *(c--)=(char)0;}
  344.  
  345.  
  346. /* PUBLIC                        HTAA_parseArgList()
  347. **        PARSE AN ARGUMENT LIST GIVEN IN A HEADER FIELD
  348. ** ON ENTRY:
  349. **    str    is a comma-separated list:
  350. **
  351. **            item, item, item
  352. **        where
  353. **            item ::= value
  354. **                   | name=value
  355. **                   | name="value"
  356. **
  357. **        Leading and trailing whitespace is ignored
  358. **        everywhere except inside quotes, so the following
  359. **        examples are equal:
  360. **
  361. **            name=value,foo=bar
  362. **             name="value",foo="bar"
  363. **              name = value ,  foo = bar
  364. **               name = "value" ,  foo = "bar"
  365. **
  366. ** ON EXIT:
  367. **    returns    a list of name-value pairs (actually HTAssocList*).
  368. **        For items with no name, just value, the name is
  369. **        the number of order number of that item. E.g.
  370. **        "1" for the first, etc.
  371. */
  372. PUBLIC HTAssocList *HTAA_parseArgList ARGS1(char *, str)
  373. {
  374.     HTAssocList *assoc_list = HTAssocList_new();
  375.     char *cur = NULL;
  376.     char *name = NULL;
  377.     int index = 0;
  378.  
  379.     if (!str) return assoc_list;
  380.  
  381.     while (*str) {
  382.     SKIPWS(str);                /* Skip leading whitespace */
  383.     cur = str;
  384.     index++;
  385.  
  386.     while (*cur  &&  *cur != '='  &&  *cur != ',')
  387.         cur++;    /* Find end of name (or lonely value without a name) */
  388.     KILLWS(cur);    /* Kill trailing whitespace */
  389.  
  390.     if (*cur == '=') {            /* Name followed by a value */
  391.         *(cur++) = (char)0;            /* Terminate name */
  392.         StrAllocCopy(name, str);
  393.         SKIPWS(cur);            /* Skip WS leading the value */
  394.         str = cur;
  395.         if (*str == '"') {            /* Quoted value */
  396.         str++;
  397.         cur = str;
  398.         while (*cur  &&  *cur != '"') cur++;
  399.         if (*cur == '"')
  400.             *(cur++) = (char)0;    /* Terminate value */
  401.         /* else it is lacking terminating quote */
  402.         SKIPWS(cur);            /* Skip WS leading comma */
  403.         if (*cur == ',') cur++;        /* Skip separating colon */
  404.         }
  405.         else {                /* Unquoted value */
  406.         while (*cur  &&  *cur != ',') cur++;
  407.         KILLWS(cur);            /* Kill trailing whitespace */
  408.         if (*cur == ',')
  409.             *(cur++) = (char)0;
  410.         /* else *cur already NULL */
  411.         }
  412.     }
  413.     else {    /* No name, just a value */
  414.         if (*cur == ',') 
  415.         *(cur++) = (char)0;        /* Terminate value */
  416.         /* else last value on line (already terminated by NULL) */
  417.         StrAllocCopy(name, "nnn");    /* Room for item order number */
  418.         sprintf(name, "%d", index); /* Item order number for name */
  419.     }
  420.     HTAssocList_add(assoc_list, name, str);
  421.     str = cur;
  422.     } /* while *str */
  423.  
  424.     return assoc_list;
  425. }
  426.  
  427.  
  428.  
  429. /************** HEADER LINE READER -- DOES UNFOLDING *************************/
  430.  
  431. #define BUFFER_SIZE    1024
  432.  
  433. PRIVATE char buffer[BUFFER_SIZE + 1];
  434. PRIVATE char *start_pointer = buffer;
  435. PRIVATE char *end_pointer = buffer;
  436. PRIVATE int in_soc = -1;
  437.  
  438. /* PUBLIC                        HTAA_setupReader()
  439. **        SET UP HEADER LINE READER, i.e. give
  440. **        the already-read-but-not-yet-processed
  441. **        buffer of text to be read before more
  442. **        is read from the socket.
  443. ** ON ENTRY:
  444. **    start_of_headers is a pointer to a buffer containing
  445. **            the beginning of the header lines
  446. **            (rest will be read from a socket).
  447. **    length        is the number of valid characters in
  448. **            'start_of_headers' buffer.
  449. **    soc        is the socket to use when start_of_headers
  450. **            buffer is used up.
  451. ** ON EXIT:
  452. **    returns        nothing.
  453. **            Subsequent calls to HTAA_getUnfoldedLine()
  454. **            will use this buffer first and then
  455. **            proceed to read from socket.
  456. */
  457. PUBLIC void HTAA_setupReader ARGS3(char *,    start_of_headers,
  458.                    int,        length,
  459.                    int,        soc)
  460. {
  461.     start_pointer = buffer;
  462.     if (start_of_headers) {
  463.     strncpy(buffer, start_of_headers, length);
  464.     buffer[length] = (char)0;
  465.     end_pointer = buffer + length;
  466.     }
  467.     else {
  468.     *start_pointer = (char)0;
  469.     end_pointer = start_pointer;
  470.     }
  471.     in_soc = soc;
  472. }
  473.  
  474.  
  475. /* PUBLIC                        HTAA_getUnfoldedLine()
  476. **        READ AN UNFOLDED HEADER LINE FROM SOCKET
  477. ** ON ENTRY:
  478. **    HTAA_setupReader must absolutely be called before
  479. **    this function to set up internal buffer.
  480. **
  481. ** ON EXIT:
  482. **    returns    a newly-allocated character string representing
  483. **        the read line.  The line is unfolded, i.e.
  484. **        lines that begin with whitespace are appended
  485. **        to current line.  E.g.
  486. **
  487. **            Field-Name: Blaa-Blaa
  488. **             This-Is-A-Continuation-Line
  489. **             Here-Is_Another
  490. **
  491. **        is seen by the caller as:
  492. **
  493. **    Field-Name: Blaa-Blaa This-Is-A-Continuation-Line Here-Is_Another
  494. **
  495. */
  496. PUBLIC char *HTAA_getUnfoldedLine NOARGS
  497. {
  498.     char *line = NULL;
  499.     char *cur;
  500.     int count;
  501.     BOOL peek_for_folding = NO;
  502.  
  503.     if (in_soc < 0) {
  504.     fprintf(stderr, "%s %s\n",
  505.         "HTAA_getUnfoldedLine: buffer not initialized",
  506.         "with function HTAA_setupReader()");
  507.     return NULL;
  508.     }
  509.  
  510.     for(;;) {
  511.  
  512.     /* Reading from socket */
  513.  
  514.     if (start_pointer >= end_pointer) {/*Read the next block and continue*/
  515.         count = NETREAD(in_soc, buffer, BUFFER_SIZE);
  516.         if (count <= 0) {
  517.         in_soc = -1;
  518.         return line;
  519.         }
  520.         start_pointer = buffer;
  521.         end_pointer = buffer + count;
  522.         *end_pointer = (char)0;
  523. #ifdef NOT_ASCII
  524.         cur = start_pointer;
  525.         while (cur < end_pointer) {
  526.         *cur = TOASCII(*cur);
  527.         cur++;
  528.         }
  529. #endif /*NOT_ASCII*/
  530.     }
  531.     cur = start_pointer;
  532.  
  533.  
  534.     /* Unfolding */
  535.     
  536.     if (peek_for_folding) {
  537.         if (*cur != ' '  &&  *cur != '\t')
  538.         return line;    /* Ok, no continuation line */
  539.         else        /* So this is a continuation line, continue */
  540.         peek_for_folding = NO;
  541.     }
  542.  
  543.  
  544.     /* Finding end-of-line */
  545.  
  546.     while (cur < end_pointer && *cur != '\n') /* Find the end-of-line */
  547.         cur++;                  /* (or end-of-buffer).  */
  548.  
  549.     
  550.     /* Terminating line */
  551.  
  552.     if (cur < end_pointer) {    /* So *cur==LF, terminate line */
  553.         *cur = (char)0;        /* Overwrite LF */
  554.         if (*(cur-1) == '\r')
  555.         *(cur-1) = (char)0;    /* Overwrite CR */
  556.         peek_for_folding = YES;    /* Check for a continuation line */
  557.     }
  558.  
  559.  
  560.     /* Copying the result */
  561.  
  562.     if (line)
  563.         StrAllocCat(line, start_pointer);    /* Append */
  564.     else
  565.         StrAllocCopy(line, start_pointer);    /* A new line */
  566.  
  567.     start_pointer = cur+1;    /* Skip the read line */
  568.  
  569.     } /* forever */
  570. }
  571.  
  572.  
  573.  
  574.  
  575.